// ==UserScript==
// @name         [kesai]chess.com装逼哈哈
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.chess.com/live
// @grant        none
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    $(".main-board-component").bind("DOMNodeInserted", changes);
    $(".sidebar-component").bind("DOMNodeInserted", enableEmoticons);


    function addGMTitle() {
        console.log("addGMTitle");
        //给自己的名字加上gm标题
        var gm = $('<a href="/members/titled_players" target="_blank" class="user-tagline-title">GM</a>');
        var im = $('<a href="/members/titled_players" target="_blank" class="user-tagline-title">IM</a>');
        var fm = $('<a href="/members/titled_players" target="_blank" class="user-tagline-title">FM</a>');
        if ($('.user-tagline-username[href="/member/kesai_chess"]').prev().length <= 0)
            $('.user-tagline-username[href="/member/kesai_chess"]').before(gm);

        //add gm title for komodo computer
        if ($('.user-tagline-username[href*="/member/Komodo"]').length > 0) {
            var Komodo_level = $('.user-tagline-username[href*="/member/Komodo"]').attr('href').replace("/member/Komodo", "")
            if ($('.user-tagline-username[href="/member/Komodo' + Komodo_level + '"]').prev().length > 0) {
                //$('.user-tagline-username[href="/member/Komodo' + Komodo_level + '"]').prev().text("大神啊");
                return;
            }
            if (Komodo_level < 8) {
                $('.user-tagline-username[href="/member/Komodo' + Komodo_level + '"]').before(fm);
            } else if (Komodo_level > 15) {
                $('.user-tagline-username[href="/member/Komodo' + Komodo_level + '"]').before(gm);
            } else {
                $('.user-tagline-username[href="/member/Komodo' + Komodo_level + '"]').before(im);
            }
        }
    }

    function fireKeyEvent(el, evtType, keyCode) {
        var doc = el.ownerDocument,
            win = doc.defaultView || doc.parentWindow,
            evtObj;
        if (doc.createEvent) {
            if (win.KeyEvent) {
                evtObj = doc.createEvent('KeyEvents');
                evtObj.initKeyEvent(evtType, true, true, win, false, false, false, false, keyCode, 0);
            } else {
                evtObj = doc.createEvent('UIEvents');
                Object.defineProperty(evtObj, 'keyCode', {
                    get: function() { return this.keyCodeVal; }
                });
                Object.defineProperty(evtObj, 'which', {
                    get: function() { return this.keyCodeVal; }
                });
                evtObj.initUIEvent(evtType, true, true, win, 1);
                evtObj.keyCodeVal = keyCode;
                if (evtObj.keyCode !== keyCode) {
                    console.log("keyCode " + evtObj.keyCode + " 和 (" + evtObj.which + ") 不匹配");
                }
            }
            el.dispatchEvent(evtObj);
        } else if (doc.createEventObject) {
            evtObj = doc.createEventObject();
            evtObj.keyCode = keyCode;
            el.fireEvent('on' + evtType, evtObj);
        }
    }


    function enableEmoticons() {
        console.log("enableEmoticons")
        //print emotion codes
        /*$('.popup-disabledIcons').children(".chat-emoticon-select").each(function() {
            console.log($(this).attr("class").replace("chat-emoticon-select chat-emoticon-select-", ":"));
        });*/
        $('.popup-disabledIcons').children(".chat-emoticon-select").unbind();
        $('.popup-disabledIcons').children(".chat-emoticon-select").bind("click", function() {
            var input = $('.chat-input-component').find('input');
            input.val(input.val()+" "+$(this).attr("class").replace("chat-emoticon-select chat-emoticon-select-", ":"))  
            //$(".popup-popup").css("display","none");          
        });
        $('.chat-emoticon-disabled').removeClass('chat-emoticon-disabled');
        vipChatName();


    }

    function vipChatName() {
        if ($('.chat-message-content-username[href="/member/kesai_chess"]').length > 0) {
            var user = $('.chat-message-content-username[href="/member/kesai_chess"]');
            user.parent().addClass("chat-message-content-diamond");
            user.parent().removeClass("chat-message-content-basic");

            user.each(function() {
                if ($(this).prev().length > 0) return;
                var gm = $('<a href="/members/titled_players" target="_blank" class="user-tagline-title">GM</a>');
                $(this).before(gm);
                $(this).addClass('username');
                //$(this).css('color', '#52b0dc');
                $(this).css('color', '#0381b8');
            });
        }
    }

    function changes() {
        addGMTitle();
    }

})();